package com.qiantu.phone.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.huawei.hms.common.util.Base64Utils;
import com.huawei.hms.framework.common.StringUtils;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.utils.rsa.JAVARSAUtil;

import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;

public class CheckPwdActivity extends AppActivity implements TextWatcher {

    private com.qiantu.phone.widget.EditTextLayout mEditText1;
    private android.widget.TextView mBtn;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_pwd;
    }

    @Override
    protected void initView() {
        mEditText1 = findViewById(R.id.edit_text1);
        mBtn = findViewById(R.id.btn);
        mEditText1.getEditText().addTextChangedListener(this);
        setOnClickListener(mBtn);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mBtn){
            LLHttpManager.checkPwd(getAppActivity(), mEditText1.getText().toString(), new HttpCallback<HttpData<Void>>(getAppActivity()){
                @Override
                public void onSucceed(HttpData<Void> result) {
                    super.onSucceed(result);
                    ChangePwdActivity.startFromReset(getAppActivity(), mEditText1.getText().toString());
                    post(() -> finish());
                }

                @Override
                public void onFail(Exception e) {
                    toast(e.getMessage());

                }
            });
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mBtn.setEnabled(!TextUtils.isEmpty(s.toString()));
    }

    private String cipherText;
    private String cipherText1;
    private String mpublicKey;
    public void rsa(View view) {
        LLHttpManager.rsa(getAppActivity(), "123456", new HttpCallback<HttpData<LLHttpManager.RSA>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<LLHttpManager.RSA> result) {
                super.onSucceed(result);
                cipherText = result.getData().cipherText;
                mpublicKey = result.getData().publicKey;
            }
        });


    }

    public void re_rsa(View view) {
        LLHttpManager.re_rsa(getAppActivity(), cipherText, new HttpCallback<HttpData<LLHttpManager.RE_RSA>>(getAppActivity()){

        });
    }

    public void rsa1(View view) {
        try {
            String publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK0/7M0UG6n/GH8Kl02dIvPqs0" +
                    "GI1rGFzaGnRl/mIg9Gg5Gml3UYcCNOlUexaPbETgR3KHEN058kdzao63/M60aNeg" +
                    "61+PJxZgPJCELaECVEtr9krdTfDWsqYp733qIwr5sOS26zOzD2jdMoUhetBWqnxv" +
                    "Ko/EbQLbUMNKyjwdaQIDAQAB";

            byte[] b2 = publicKeyString.getBytes();

            byte[] ss = JAVARSAUtil.encryptByPublicKey("123456".getBytes(), Base64.decode(publicKeyString, Base64.DEFAULT));
            cipherText1 = Base64.encodeToString(ss, Base64.DEFAULT);

            LLHttpManager.re_rsa(getAppActivity(), cipherText1, new HttpCallback<HttpData<LLHttpManager.RE_RSA>>(getAppActivity()){
                @Override
                public void onSucceed(HttpData<LLHttpManager.RE_RSA> result) {
                    super.onSucceed(result);
                }
            });
            Log.i("rsa1", cipherText1);
            Log.i("equal", String.valueOf(cipherText1.equals(cipherText)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void rsa2(View view) {
        String publicKeyString = "ytP+zNFBup/xh/CpdNnSLz6rNBiNaxhc2hp0Zf5iIPRoORppd1GHAjTpVHsWj2xE4EdyhxDdOfJHc2qOt/zOtGjXoOtfjycWYDyQhC2hAlRLa/ZK3U3w1rKmKe996iMK+bDktuszsw9o3TKFIXrQVqp8byqPxG0C21DDSso8HWk=";
        byte[] b1 = Base64.decode(publicKeyString, Base64.NO_WRAP);
        String s1 = Base64.encodeToString(b1, Base64.NO_WRAP);
        boolean f1 = s1.equals(publicKeyString);

        int keyLength = 1024;
        try {
            //生成密钥对
            KeyPair keyPair = JAVARSAUtil.generateRSAKeyPair(keyLength);

            //获取公钥
            byte[] publicKey = JAVARSAUtil.getPublicKey(keyPair);
            Log.d("TAG", "公钥：" + StringUtils.byte2Str(publicKey));
            //公钥用base64编码
            String encodePublic = Base64Utils.encode(publicKey);
            Log.d("TAG", "base64编码的公钥：" + encodePublic);

            //获取私钥
            byte[] privateKey = JAVARSAUtil.getPrivateKey(keyPair);
            Log.d("TAG", "私钥：" + StringUtils.byte2Str(privateKey));
            //私钥用base64编码
            String encodePrivate = Base64Utils.encode(privateKey);
            Log.d("TAG", "base64编码的私钥：" + encodePrivate);

            try {
                byte[] result = JAVARSAUtil.encryptByPublicKey("111111".getBytes(), publicKey);
                String ss1 = Base64Utils.encode(result);
                byte[] result1 = Base64Utils.decode(ss1);

                byte[] re = JAVARSAUtil.decryptByPrivateKey(result, privateKey);
                String ss2 = StringUtils.byte2Str(re);
                String ss3 = Base64Utils.encode(re);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        /*String ss = "";
        try {
            RSACipherUtil rsaCipherUtil = new RSACipherUtil();
            ss = rsaCipherUtil.encrypt("123456");
            Log.i("rsa2", ss);
            Log.i("equal", String.valueOf(ss.equals(cipherText)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    public void rsa3(View view) {
        LLHttpManager.rsa_pk(getAppActivity(), "123456", new HttpCallback<HttpData<LLHttpManager.RSA>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<LLHttpManager.RSA> result) {
                super.onSucceed(result);
                cipherText = result.getData().cipherText;
                mpublicKey = result.getData().publicKey;
            }
        });
    }
}